package com.shop.clientorder.chainduty.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.clientorder.chainduty.Processor;
import com.shop.clientorder.chainduty.kernel.ProcessorChain;
import com.shop.clientorder.chainduty.pojo.StockAttr;
import com.shop.clientorder.feign.ProductRemote;
import com.shop.common.core.domain.Result;
import com.shop.common.core.exception.GlobalException;
import com.shop.common.redis.service.RedisService;
import com.shop.product.domain.PmsSkuInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：作者姓名
 * @Package：com.shop.clientorder.chainduty.impl
 * @Project：e-commerce-services
 * @name：StockChheckProcessor
 * @Date：2024/7/8 9:08
 */
@Component
public class StockChheckProcessor implements Processor {
    @Autowired
    private RedisService redisService;
    @Autowired
    private ProductRemote productRemote;
    @Override
    public Result<Boolean> process(StockAttr stockAttr, ProcessorChain chain) {
        Integer count=0;
        //库存校验
        stockCheckout(stockAttr.getSkuId(),stockAttr.getDetailsCount());
        //遍历获取到的信息
        List<String> cacheList = redisService.getCacheList("" + stockAttr.getSkuId());
        for (String s : cacheList) {
            if (!"is null".equals(s)) {
                count++;
            }
        }
        redisService.deleteObject("" + stockAttr.getSkuId());
        // 检验不通过
        if (count == 0) {
            // 查询商品详细信息
            Result<PmsSkuInfo> skuInfo = productRemote.getSkuInfo(stockAttr.getSkuId());
            PmsSkuInfo pmsSkuInfo = skuInfo.getData();
            return Result.error(false, JSONObject.toJSONString(pmsSkuInfo));
            // throw new RuntimeException("商品“" + info.getData().getSkuTitle() + "”库存不足");
        }
        // 检验通过
        else {
            return chain.process(stockAttr, chain);
        }
    }

    //库存校验
    private void stockCheckout(Long skuId, Long detailsCount) {
        //创建String类型集合，存储空值，
        ArrayList<String> stockCheckList = new ArrayList<>();
        //从redis中获取商品库存
        if(!redisService.hasKey("")){
            throw new GlobalException("无法从redis中获取库存");
        }
        List<Long> skuIds = redisService.getCacheList("");
        for (Long id : skuIds) {
            String stockString = redisService.getCacheMapValue("aaa" + id, skuId.toString());
            Long stock = JSONObject.parseObject(stockString, Long.class);
            if (null == stock) {
                // 存入空值
                stockCheckList.add("is null");
                // 结束当前循环
                continue;
            }
            // 2. 判断数量是否小于库存量
            boolean bool = stock < detailsCount;
            // 2.1. 如果库存量小于需求 返回提示库存不足
            if (bool) {
                // 存入空值
                stockCheckList.add("is null");
            }
            // 2.2. 如果库存量大于等于需求 存入库存值
            else {
                stockCheckList.add(stockString);
            }
        }
        // 存入库存集合 值为null(库存为0或不存在)或库存数量
        redisService.setCacheList("" + skuId, stockCheckList);
    }
}
